/**
 * 相似受众管理
 */
define(['../../common/service', '../../common/utils', 'jquery-range'], function (http) {
    //业务处理
    var LookAlikeAu = function () {
        this.step = 1;   //记录创建的步数
        this.step_s = 1;   //记录创建的步数
        this.similar_country_top = 72;
        this.similar_au_top = 104;
        this.countrySelect = null;
    };
    LookAlikeAu.prototype = $.extend(LookAlikeAu.prototype, {
        _initSearchEvent: function () {
            var _this = this;
            //查询btn
            $('#au-search-btn').unbind('click').on('click', function () {
                _this._query.cPage = 1;
                _this.getList(true);
            });
        },
        init: function () {
            var _this = this;
            _this._initTabs();
            $('[data-toggle="tooltip"]').tooltip();
            _this._initSearchEvent();
            //列表管理
            $('table[list-manager="lookAlikeAudienceManager"]').listManager('init', {
                setTopVal:184//调整吸顶的高度,需要减去这个高度
                ,scrollDOM:$('.fmp-article')//监听滚动条
                ,supportAjaxPage: true
                ,isDevelopMode: false		//是否为开发模式，为true时将打印事件日志
                , basePath: ''			//当前基本路径[用于加载资源文件]
                , useDefaultStyle: false		//是否使用默认的table样式
                , animateTime: 300		//动画效果延时
                , minNum: 40					//宽度所允许的最小值
                , supportAdjust: true		//是否支持宽度调整
                , supportDrag: false 		//是否支持拖拽换位
                , supportConfig: false 		//是否支持配置列表功能[操作列是否可见]
                , supportSetTop: true		//是否支持表头置顶
                , autoLoadCss: false
                , pageCallback: function (query) {
                    $.extend(_this._query, query);
                    _this.getList();
                }
            }, function (query) {
                _this._query = {
                    pSize: query.pSize,
                    cPage: 1
                };
                _this.getList();			//获取列表数据
            });
            //绑定 创建按钮
            _this._initLookALikeDialog();
            _this._initSelect();
            _this._initCheckRedio();
            _this._saveEvent();
            _this._bindDeleteBtnEvent();
        }
        , getList: function (flag) {
            var _this = this;
            var loadingArea = $('#au-grid');
            //Kit.loading.show(loadingArea);
            var par = _this._query;
            par.name = $('input[name=searchName]').val();
            http.do('/looklikeaudience/list', {param: JSON.stringify(par)}, 'POST').then(function (res) {
                var data = res.data;
                //操作DOM节点更换数据
                var _table = $('table[list-manager="lookAlikeAudienceManager"]'),
                    _tbody = $('#au-grid', _table).empty(),
                    tmpHtml = '',
                    _list = data.customaudiences;
                if(flag) {
                    if (!_list || _list.length == 0) {
                        _table.listManager([{'resetTd': false, 'resetPageData': data.paginator}]);
                        console.log('列表为空，已返回');
                        tmpHtml += '<tr><td colspan="6">'
                            + I18N.getText('lookalikeAudience','noSearchMsg')+'</td></tr>';
                        _tbody.html(tmpHtml);
                        return false;
                    }
                } else {
                    if (_list.length == 0) {
                        tmpHtml += '<tr><td colspan="6" style="font-size: 34px;color: #777;height: 211px;">'
                            + I18N.getText('lookalikeAudience','noCreateMsg')+'</td></tr>';
                        _tbody.html(tmpHtml);
                    }
                }

                if (data.paginator.length == 0) {
                    console.log('分页数据为空，已返回');
                    return false;
                }

                for (var i = 0; i < _list.length; i++) {
                    tmpHtml += '<tr data-id="' + _list[i].id + '">'
                        + '<td title="' + (i + 1) + '">'
                        + (i + 1)
                        + '</td>'
                        + '<td width="100" class="text-left cu-name">'
                        + _list[i].name
                        + '</td>'
                        + '<td width="100" class="text-center">'
                        + _list[i].approximateCount
                        + '</td>'
                            //+ '<td width="100" class="text-center">'
                            //+ (_list[i].approximateCount==0 ? '--': (_list[i].approximateCount)+ '人')
                            //+ '</td>'
                        + '<td width="100" class="text-center">'
                        + _list[i].availableAccountNum + '/ ' + +_list[i].accountNum
                        + '</td>'
                        + '<td width="100" class="text-center">'
                        + new Date(_list[i].timeUpdated).format('yyyy-MM-dd hh:mm:ss')
                        + '</td>'
                        + '<td width="100" class="text-center">'
                        + '<a name="delete" href="javascript:void(0);">'
                        + '<span class="glyphicon glyphicon-trash"></span></a>'
                        //+ '<a name="delete" href="#">'+I18N.getText('lookalikeAudience','delete')+'</a>'
                        + '<input type="hidden" name="templateId" value="'+_list[i].id+'"></td>'
                        + '</tr>';
                }
                _tbody.html(tmpHtml);
                //Kit.loading.hide(loadingArea);
                _table.listManager([{'resetTd': false, 'resetPageData': data.paginator}]);
                //绑定列表的时间
                _this._bindTableColumnEvent();
            });
        }
        , _initLookALikeDialog: function () {
            var _this = this;
            // init select
            $("#au-create-btn").unbind('click').bind('click', function () {
                $('#create-au-dialog').modal({backdrop: 'static', keyboard: false});
                _this._initDialog();
            });
            // init range js
            var $dom = $('input[name=similar-au]');
            _this._initRangeWidget($dom, function (v) {
                $dom.parent('div').parent('div').find('span[name="au-template-num"]').html(v);
                $('.similar-box').find('select').each(function(){
                    var total = $(this).find("option:selected").attr("no");
                    $(this).parent().find('span[name=similar-box-num]').html(_this.calculateNumOfPeople_au(v,total));
                });
            });
            _this._addBox();
            _this._bindBoxAddEvent();
        }

        , _initAudienceSelect: function (callback) {
            var _this = this;
            //获取seelct应用列表
            http.do("/looklikeaudience/getOriginList", {param: ''}, 'POST').then(function (res) {
                var data = res.data;
                var tmpHtml = '<option value="-1">'+I18N.getText('lookalikeAudience','selectAudience')+'</option>';
                $.each(data, function (index, dict) {
                    tmpHtml += '<option value="' + dict.id + '">' + dict.name + '</option>'
                });
                $("select[name='custom-au']").html(tmpHtml);
                var $select = $("select[name='custom-au']").select2({
                    placeholder: I18N.getText('lookalikeAudience','selectAudience'),
                    escapeMarkup: function (m) {
                        return m;
                    }
                });
                if (typeof callback != "undefined") {
                    $select.on("select2:select", function (e) {
                        callback.call(_this, this);
                    });
                }
                return $select;
            });
        }
        , audienceSelectCallback: function (obj) {
            if($(obj).val() == -1){
                $('span[name=cus-au-name]').html('');
            }else{
                var name = $("select[name='custom-au']").find('option:selected').html();
                $('span[name=cus-au-name]').html(' - ' + name);
            }
        }
        , _initFansSelect: function () {
            //获取seelct应用列表
            http.do("/page/getPages", {param: ''}, 'GET').then(function (res) {
                var data = res.data;
                var tmpHtml = '';
                $.each(data, function (index, dict) {
                    tmpHtml += '<option img="'+(dict.pictureUrl || '')+ '" value="' + dict.fbPageId + '">' + dict.name + '</option>'
                });
                $("select[name='custom-au']").html(tmpHtml);
                var $select = $("select[name='custom-au']").select2({
                    placeholder: I18N.getText('lookalikeAudience','selectPage'),
                    templateResult: formatState,
                    templateSelection:formatState,
                    escapeMarkup: function (m) {
                        return m;
                    }
                });
                function formatState(state) {
                    var t = $(state.element).attr("img");
                    if(!t){
                        t = '';
                    }
                    var $state =$(
                        '<span><img src="'+t +'" class="img-flag" width="20" height="20" /> '+state.text+ '</span>');
                    return $state;
                };


                return $select;
            });
        }
        , _initCountrySelect: function ($Dom, callback) {
            var _this = this;
            //初始化select2
            http.do("/search/searchALLAdgeolocation", {param: ''}, 'GET').then(function (res) {
                var data = res.data;
                var tmpHtml = '<option no="1" value="-1">'+I18N.getText('lookalikeAudience','selectCountry')+'</option>';
                $.each(data, function (index, dict) {
                    tmpHtml += '<option no="' + (dict.fbUsersNumber || '') + '"  value="' + (dict.key || '') + '">'
                        + (dict.name || '') + '&nbsp;(' + (dict.key || '') + ')</span>';
                    +'</option>'
                });
                //$("select[name='similar-country-au']").html(tmpHtml);
                $Dom.html(tmpHtml);
                //var $similarSelect = $("select[name='similar-country-au']").select2({
                var $similarSelect = $Dom.select2({
                    placeholder: I18N.getText('lookalikeAudience','selectCountry'),
                    escapeMarkup: function (m) {
                        return m;
                    }
                });
                if (typeof callback != "undefined") {
                    $similarSelect.on("select2:select", function (e) {
                        callback.call(_this, this);
                    });
                }
                return $similarSelect;
            })
        }
        , _initSelect: function () {
            var _this = this;
            //_this._initAudienceSelect(_this.audienceSelectCallback);
            //_this._initFansSelect();
            var $select = _this._initCountrySelect($("select[name='similar-country-au']"), _this.countrySelectCallback);
        }
        /* 选择国家后修改人数   */
        , countrySelectCallback: function (obj) {
            var _this = this;
            _this._changeSelectInCountryEvent();
            if($(obj).val() == "-1"){
                $('.counts-ul').hide();
                //$('div[name=box-text]').hide();
            }else{
                //更新 国家码
                $('span[name="au-template-name"]').html($(obj).val());
                var countryTotalPepleNo = $(obj).find("option:selected").attr("no"); //国家总人数
                var arr = _this.calculateNumOfPeople(countryTotalPepleNo);
                $('.counts-ul .step-no').each(function (i) {
                    $(this).html(arr[i]);
                });
                $('.counts-ul').show();
            }
        }
        , _initRangeWidget: function ($Dom, callback) {
            $Dom.jRange({
                from: 0,//开始于
                to: 10,//结束于
                step: 0.1,//一次滑动多少
                scale: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],//分割点
                format: '%s',//格式化格式
                width: 450,//宽度
                showLabels: false,
                showScale: true,
                callback: callback
            });
        }
        , _bindBoxAddEvent: function () {
            var _this = this;
            $('span[name=similar-city-add]').unbind('click').bind('click', function () {
                _this._addBox();
                //显示X按钮
                var name = "box-" + (_this.step -1);
                var s = "input[name=" + name + "]";
                $(s).parent('div').nextAll('button').show();
                _this._changeSelectInCountryEvent();
                //移动add块高度
                $(this).css('top', ($(this).position().top + 100));
            });
            $('span[name=similar-au-add]').unbind('click').bind('click', function () {
                var selectName = _this._addSimilarBox();
                selectName = 'select[name="' + selectName + '"]';
                //初始化国家下拉菜单
                _this._sameRadioAddCountrySelect($(selectName));
                //显示X按钮
                var name = "box-select-" + (_this.step_s-1);
                var s = "select[name=" + name + "]";
                $(s).nextAll('button').show();
                //$('.bc-box').clone(true).eq(0).insertAfter(this);
                //$('.bc-box').position().top; 27
                //$('.bc-box').height()   78
                //移动add块高度
                $(this).css('top', ($(this).position().top + 100));
            });
        }
        , _initCheckRedio: function () {
            var _this = this;
            $("#create-au-dialog").find("input[name='radio1']").unbind('click').bind('click', function () {
                if ($(this).val() == 1) {
                    _this._initAudienceSelect(_this.audienceSelectCallback);
                } else {
                    _this._initFansSelect();
                }
            });
            $("#create-au-dialog").find("input[name='radio2']").unbind('click').bind('click', function () {
                _this.removeBox();
                if ($(this).val() == 0) {
                    $("div[name='similar-au']").hide();
                    $('span[name=similar-au-add]').hide();
                    $('span[name=similar-city-add]').show();
                    $('div[name=country-select-div]').show();
                    $('.counts-ul').hide();
                    $(".counts-div").show();
                    $("div[name='bc-box").show();
                    //初始化select
                    $("select[name='similar-country-au']").val('-1').trigger('change');
                    //添加 bc-box
                    _this._addBox();
                    //初始化 addBtn
                     $('span[name=similar-city-add]').css("top",_this.similar_country_top);
                } else {
                    $("div[name='bc-box").hide();
                    $(".counts-div").hide();
                    $('div[name=country-select-div]').hide();
                    $('span[name=similar-city-add]').hide();
                    $('span[name=similar-au-add]').show();
                    $("div[name='similar-au").show();
                    //添加 similarBox
                    var selectName = _this._addSimilarBox();
                    selectName = 'select[name="' + selectName + '"]';
                    //初始化国家下拉菜单
                    _this._sameRadioAddCountrySelect($(selectName));
                    // init btn
                    //初始化 addBtn
                    $('span[name=similar-au-add]').css("top",_this.similar_au_top);

                    //初始化range插件值
                    $('.similar-range').html('<input name="similar-au" type="hidden" value="1" style="display: none;">');
                    var $dom = $('input[name=similar-au]');
                    _this._initRangeWidget($dom, function (v) {
                        $dom.parent('div').parent('div').find('span[name="au-template-num"]').html(v);
                        $('.similar-box').find('select').each(function(){
                            var total = $(this).find("option:selected").attr("no");
                            $(this).parent().find('span[name=similar-box-num]').html(_this.calculateNumOfPeople_au(v,total));
                        });
                    });
                }
            });
        }
        ,showTextOfSimilarAu:function($select){
            var _this = this;
            //更改数字
            var num = $('input[name=similar-au]').val();
            var counts = $select.find("option:selected").attr("no");
            if($select.val() == -1){
                //显示 初始化text 隐藏覆盖人数
                $select.parent().find('.similar-span').hide();
                $select.parent().find('.similar-text-div').html(I18N.getText('lookalikeAudience','countryTooLittleMsg'));
            }else{
                $select.parent().find('span[name=similar-box-num]').html(_this.calculateNumOfPeople_au(num,counts));
                $select.parent().find('.similar-span').show();
                var tempHtml = I18N.getText('lookalikeAudience','likeAudience') + ': Lookalike(<span name="au-template-name">' + $select.val() + '</span>,<span name="au-template-num">'+ num +'</span>%) <span name="cus-au-name"></span></div>';
                $select.parent().find('.similar-text-div').html(tempHtml);
            }
        }
        , _sameRadioAddCountrySelect : function($select){
            var _this = this;
            _this._initCountrySelect($select,function(dom){
                _this.showTextOfSimilarAu($select);
                _this._changeSelectInCountryEvent();
            });
        }
        , _addSimilarBox: function () {
            var _this = this;
            if($('.similar-box').length > 0){
                $('.similar-box').find('.box-close').show();
            }
            var name = "box-select-" + _this.step_s;
            $('.similar-out').append(_this._templateSimilarHTML(name));
            _this.step_s++;
            _this.closeBtnEvent('');
            return name;
        }
        , _addBox: function () {
            var _this = this;
            if($('.bc-box').length > 0){
                $('.bc-box').find('.box-close').show();
            }
            var name = "box-" + _this.step;
            var $select = $('select[name=similar-country-au]');
            $('.bc-box-out').append(_this._templateHTML(name, $select.val()));
            var s = "input[name=" + name + "]";
            _this._initRangeWidget($(s), function (v) {
                var dom = $(s).parent('div').parent('div').find('span[name="au-template-num"]');
                dom.html(v);
            });
            _this.step++;
            _this.closeBtnEvent('country');
        }
        , _templateHTML: function (name, t_name) {
            return ' <div name="bc-box" class="panel panel-default bc-box">'
                + '<div class="box-range">'
                + ' <input name="' + name + '" class="box-range-input" type="hidden" value="1" />'
                + ' </div>'
                + '  <span class="box-c">%</span>'
                + '   <button type="button" class="close box-close" style="display:none;"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>'
                + ' <div name="box-text" style="display: none;">'+I18N.getText('lookalikeAudience','likeAudience')+': Lookalike(<span name="au-template-name">' + t_name + '</span>,<span name="au-template-num">0</span>%)<span name="cus-au-name"></span></div>'
                + '</div>'
        }
        , _templateSimilarHTML: function (name) {
            return '<div class="similar-box">'
                + '<select class="template-drop-selector js-states form-control accessible"  name="' + name + '"></select>'
                + '<span class="similar-span" style="display:none;">'+I18N.getText('lookalikeAudience','uniqueUser')+': <span name="similar-box-num">2.3k</span></span>'
                + '<button type="button" class="close box-close" style="display: none;"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>'
                + ' <div class="similar-text-div">'+I18N.getText('lookalikeAudience','countryTooLittleMsg')+'</div>'
                //+ ' <div class="similar-country-error-msg">您不能创建两组完全相同的相似受众。</div>'
                + '</div>';
        }
        ,_changeSelectInCountryEvent:function(){
            if($('select[name=similar-country-au]').val() != -1){
                $('div[name=box-text]').show();
            }else{
                $('div[name=box-text]').hide();
            }
            if($("select[name='custom-au']").val() == -1){
                $('span[name=cus-au-name]').html('');
            }else{
                var name = $("select[name='custom-au']").find('option:selected').html();
                $('span[name=cus-au-name]').html(' - ' + name);
            }
        }
        , _saveEvent: function () {
            var _this = this;
            $(".save-btn").unbind('click').bind('click', function (e) {
                var ischeckpass = true, originType = $('.origin-type-select:checked ').val(); //模板或者主页
                var custom_id = $("select[name='custom-au']").val(); //源模板id 或者主页id
                if(custom_id == '-1'){
                    Kit.alert2(I18N.getText('lookalikeAudience','selectSourceAudience'));
                    return ;
                }
                var radio_type = $('.radio-type-is-county:checked ').val(); //相同国家或者相同规模
                var params = {
                    originType: originType,
                    originId: custom_id
                };
                if (radio_type == 0) { //相同国家
                    var country_id = $("select[name='similar-country-au']").val();
                    if(country_id == '-1'){
                        Kit.alert2(I18N.getText('lookalikeAudience','selectCountry'));
                        return ;
                    }
                    var country_key = $("select[name='similar-country-au']").val();
                    var rangeArray = [];
                    var countryRatioList = [];
                    $('.box-range-input').each(function (i, input) {
                        var tmprange = $(input).val();
                        if (tmprange == 0 || $.inArray(tmprange,rangeArray) != -1) {
                            Kit.warning4($(this).parent().parent());
                            Kit.alert2(I18N.getText('lookalikeAudience','sameAudienceMsg'));
                            ischeckpass = false;
                            return;
                        } else {
                            rangeArray.push(tmprange);
                            countryRatioList.push({
                                country: country_key,
                                ratio: tmprange
                            });
                        }
                    });
                } else { //相同受众规模
                    $('.similar-box select').each(function(i){
                        if($(this).val() == '-1'){
                            Kit.warning4($(this).parent());
                            Kit.alert2(I18N.getText('lookalikeAudience','selectCountry'));
                            ischeckpass = false;
                            return false;
                        }
                    });
                    if(!ischeckpass){
                        return false;
                    }
                    var similar_au_key = $('input[name=similar-au]').val();
                    var countryRatioList = [];
                    var rangeArray = [];
                    $('div[name=similar-au]').find('select').each(function (i, selectObj) {
                        var c_name = $(selectObj).val();
                        if ($.inArray(c_name,rangeArray) != -1) {
                            Kit.warning4($(this).parent());
                            Kit.alert2(I18N.getText('lookalikeAudience','sameAudienceMsg'));
                            ischeckpass = false;
                            return;
                        } else {
                            rangeArray.push(c_name);
                            countryRatioList.push({
                                country: c_name,
                                ratio: similar_au_key
                            });
                        }
                    });
                }
                params.countryRatioList = countryRatioList;
                // save
                if (ischeckpass) {
                    http.doAsync('/looklikeaudience/save', {param: JSON.stringify(params)}, "POST").then(function (res) {
                        if (res.flag == 'fail') {
                            $.each(res.data, function (i,obj) {
                                if (radio_type == 0) { //相同国家
                                    $.each($('.box-range-input'),function(){
                                        if($(this).val() == obj.ratio){
                                            Kit.warning4($(this).parent().parent());
                                        }
                                    })
                                }else{
                                    $.each($('.similar-box select'),function(){
                                        if($(this).val() == obj.country){
                                            Kit.warning4($(this).parent());
                                        }
                                    })
                                }
                                Kit.alert2(res.msg);
                            });
                        } else {
                            Kit.alert_success(I18N.getText('lookalikeAudience','createloolalikeSuuceed'));
                            _this._closeDialog($('#create-au-dialog'));
                            _this.getList();
                        }
                    })
                }
            });
        }
        //计算10个
        , calculateNumOfPeople: function (count) {
            var arr = [];
            for (var i = 1; i <= 10; i++) {
                var temp = count * (i / 100);
                if (temp >= 1000000) {
                    arr.push(Math.round(temp / 1000000 * 10) / 10 + "M")
                } else if (temp >= 1000) {
                    arr.push(Math.round(temp / 1000 * 10) / 10 + "K")
                } else {
                    arr.push(Math.round(temp * 10) / 10);
                }
            }
            return arr;
        }
        , calculateNumOfPeople_au: function (num,count) {
            var temp = count * (num / 100);
            if (temp >= 1000000) {
                return Math.round(temp / 1000000 * 10) / 10 + "M";
            } else if (temp >= 1000) {
                return Math.round(temp / 1000 * 10) / 10 + "K";
            } else {
                return Math.round(temp * 10) / 10;
            }
        }
        , _bindTableColumnEvent: function () {
            var _this = this;
            //绑定 输入框
            //弹出层
            $("#au-grid tr").unbind('click').bind('click', function (e) {
                if (e.target.nodeName != 'TD' && e.target.nodeName != 'TR' && e.target.nodeName != 'SPAN') {
                    return false;
                }
                if(e.target.className.indexOf("glyphicon",0) != -1){
                    //如果点击的是更新或者删除按钮。
                    return false;
                }
                _this._initCollapseTable($(this));
            });
            //删除
            $("a[name='delete']").unbind('click').bind('click', function () {
                var templateId = $(this).next("input").val();
                var name = $(this).parent().parent('tr').find(".cu-name").html();
                $('#delete-au-dialog').data("id", templateId);
                $('#delete-au-dialog span[name=del-name]').each(function (i) {
                    $(this).html(name);
                });
                $('#delete-au-dialog').modal({backdrop: 'static', keyboard: false});
            });

        }
        ,removeBox:function(){
            $('.bc-box').remove();
            $('.similar-box').remove();
        }
        ,closeBtnEvent:function(flag){
            $(".box-close").unbind('click').bind('click',function(){
                $(this).parent().remove();
                if(flag == 'country'){
                    //去掉+号
                    if($('.bc-box').length == 1){
                        $(".box-close").hide();
                    }
                    $('span[name=similar-city-add]').css('top', ($('span[name=similar-city-add]').position().top - 100));
                }else{
                    //去掉+号
                    if($('.similar-box').length ==1){
                        $('.box-close').hide();
                    }
                    $('span[name=similar-au-add]').css('top', ($('span[name=similar-au-add]').position().top - 100));
                }

            });
        }
        , _closeDialog: function ($dom) {
            $dom.modal('hide');
        }
        , _initCollapseTable: function ($Dom) {
            if ($Dom.next("#ad-account-status").get(0)) {
                //已渲染
                $("#ad-account-status").remove();
                return;
            } else {
                $("#ad-account-status").remove();
            }
            var html = '<tr id="ad-account-status"><td colspan="6" name="colspanTd"><div name="collapseTable">' +
                '<table id="inner-table" class="table table-bordered table-hover"><thead>' +
                '<th width="30%">'+I18N.getText('lookalikeAudience','account')+'</th><th width="70%">'+I18N.getText('lookalikeAudience','status')+'</th></thead><tbody>' +
                '</tbody></table>' +
                '</div></td></tr>';
            $Dom.after(html);
            var _par = {id: $Dom.find("input[name='templateId']").val()};
            http.do('/customaudiencetemplate/customAudienceItemList', {param: JSON.stringify(_par)}, 'POST').then(function (res) {
                if (res.flag == "success") {
                    var _data = res.data.result;
                    var temp = "";
                    for (var i = 0; i < _data.length; i++) {
                        console.log('requestQueueStatus>'+_data[i].requestQueueStatus + ';operationStatusCode>'+_data[i].operationStatusCode+';deliveryStatusCode>'+_data[i].deliveryStatusCode)
                        temp +=I18N.getText('lookalikeAudience','uploading')+ "<tr><td>" + _data[i].fbAccountName + "</td><td>";
                        if (_data[i].requestQueueStatus == 0 || (_data[i].requestQueueStatus == 1 && _data[i].operationStatusCode == -1 && _data[i].deliveryStatusCode == -1)) {
                            temp += I18N.getText('lookalikeAudience','uploading')+"</td></tr>";
                        } else if (_data[i].requestQueueStatus == 1 && _data[i].operationStatusCode == 200 && _data[i].deliveryStatusCode == 200) {
                            temp += I18N.getText('lookalikeAudience','createSucceed')+"</td></tr>";
                        }  else {
                            if(_data[i].operationStatusCode == 200 || _data[i].operationStatusCode == -1){
                                temp += "<span style='color: red'>"+"[<span>" + _data[i].deliveryStatusDescription + "</span>]</span></td></tr>";
                            } else if(_data[i].operationStatusCode == 300) {
                                temp += I18N.getText('lookalikeAudience','updatingAudience')+"</td></tr>";
                            } else {
                                temp += "<span style='color: red'>"+I18N.getText('lookalikeAudience','uploadError')+"[<span>" + _data[i].deliveryStatusDescription + "</span>]</span></td></tr>";
                            }
                        }
                    }

                    $('#ad-account-status').find('tbody').append(temp);
                } else {
                    Kit.alert2(I18N.getText('lookalikeAudience','queryFail'));
                }
            });

        }
        , _initDialog: function () {
            $('.counts-ul').hide();
            $("#create-au-dialog").find("input[name='radio1']").eq(0).click();
            $("#create-au-dialog").find("input[name='radio2']").eq(0).click();
        }
        , cleanWarnHtml: function () {
            $("#create-au-dialog div[name='step3'] footer[name='warn']").html("");
        }
        ,_bindDeleteBtnEvent:function(){
            var _this = this;
            //删除自定义受众
            $(".delete-btn").unbind('click').bind('click',function(){
                var loadingArea = $("#delete-au-dialog");
                Kit.loading.show(loadingArea);
                var obj = this;
                _this._disabledBtn($(obj),false);
                var _ids = [];
                _ids.push(parseInt($('#delete-au-dialog').data("id")));
                var par = {
                    ids : _ids,
                    flag:$("input[name='delete-radio']:checked").val()
                }
                http.do('/looklikeaudience/del',{param:JSON.stringify(par)},'POST').then(function(res){
                    Kit.loading.hide(loadingArea);
                    _this._closeDialog($("#delete-au-dialog"));
                    if(res.flag == "success"){
                        Kit.alert_success(I18N.getText('lookalikeAudience','deleteSucceed'));
                    }else{
                        Kit.alert2(res.msg);
                    }
                    _this._disabledBtn($(obj),true);
                    _this.getList();
                });
            });
        }
        , dispose: function () {
        }
        ,_disabledBtn:function($Dom,flag){
            if(flag){
                $Dom.removeAttr("disabled")
            }else{
                $Dom.attr("disabled","disabled");
            }
        },
        _initTabs:function(){
            $("div.ndp-tab-wrapper[name=plain-tab]").tabs({
                list:[I18N.getText('customAudience','customAudience'),I18N.getText('customAudience','likeAudience')]
            }).on("TAB_CHANGE",function(e){//点击其中的一个tab发生事件
                switch (e.originalEvent.data.index){
                    case 0:
                        window.location.hash = "#!customAudience";
                        break;
                    case 1:window.location.hash = "#!lookalikeAudience";
                        break;
                }
            }).val(1);//设置选中的tab 索引，默认0
        }
    })
    return new LookAlikeAu();
});
